from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

# 封装加密的类
class SecretOauth(object):
    def __init__(self):
        self.serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=24 * 14 * 3600)

    # 加密
    def dumps(self, data):

        result = self.serializer.dumps(data)
        return result.decode()

    # 解密
    def loads(self, data):

        result = self.serializer.loads(data)
        return result

    # #加密
    # def dumps(self, data):
    #     s = Serializer(secret_key=settings.SECRET_KEY,expires_in=24 * 14 * 3600)
    #     result = s.dumps(data)
    #     return result.decode()
    # #解密
    # def loads(self, data):
    #     s = Serializer(secret_key=settings.SECRET_KEY, expires_in=24 * 14 * 3600)
    #     result = s.loads(data)
    #     return result


#
# if __name__ == '__main__':
#     s = Serializer(secret_key="abc", expires_in=3600)
#     data_dict ={
#         'a':1
#     }
#     result = s.dumps(data_dict)
#     # print(result.decode())
#
#     loads_result = s.loads(result)
#     print(loads_result)
#     print(type(loads_result))